<?php
namespace app\admin\controller;

use app\common\controller\SysAction;
use think\helper\Time;
use think\Db;

class Orders extends SysAction
{
    protected $modelClass = '\app\common\model\Orders';
    
    public function initialize()
    {
        parent::initialize();
    }

    public function index(){
        $where = [];
		if (request()->isPost()) { // 表单搜索
            $post = input("post.");
            if ($post['ordersn']) { //搜索订单号
                $where[] = ['ordersn', '=', $post['ordersn']];
            }
            if ($post['merchant']) { //搜索商家
                $agent = new \app\common\model\Agent;
                $agent_id = $agent->where("agentname",$post['merchant'])->value("id");
                if ($agent_id) {
                    $where[] = ['agent_id', '=', $agent_id];
                }else{
                    $where[] = ['agent_id', '=', 0];
                }
            }
            if ($post['m_name']) { // 搜索会员
                $where[] = ['m_name|m_phone', '=', $post['m_name']];
            }
            if ($post['status'] != "") { //搜索状态
                if ($post['status'] == '-1') {
                    $post['status'] = 0;
                }
                $where[] = ['status', '=', $post['status']];
            }
            if ($post['start_time']) { //搜索时间区间
                $start_time = strtotime($post['start_time']);
                $end_time = strtotime($post['end_time']);
                $where[] = ['create_time', 'between', [$start_time, $end_time]];
            }
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        $this->assign("dataList",$dataList);
		return $this->fetch("index");
    }


    /**
     * @Title: orderView
     * @Description: todo(订单)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    public function orderview(){
    	$data = [];
    	$data['today_order_num'] = $this->orderTotalByTime(Time::dayToNow(0)[0]);
    	$data['today_amount'] = $this->orderAmountByTime(Time::dayToNow(0)[0]);
    	$data['total_order_num'] = $this->orderTotal();
    	$data['total_amount'] = $this->orderAmount();
    	$data['order_line'] = $this->orderLine(30);
    	$data['7_days_amount'] = $this->orderAmountByTime(Time::dayToNow(7)[0]);
    	$data['7_days_order_num'] = $this->orderTotalByTime(Time::dayToNow(7)[0]);
    	$data['14_days_amount'] = $this->orderAmountByTime(Time::dayToNow(14)[0]);
    	$data['14_days_order_num'] = $this->orderTotalByTime(Time::dayToNow(14)[0]);
    	$data['30_days_amount'] = $this->orderAmountByTime(Time::dayToNow(30)[0]);
    	$data['30_days_order_num'] = $this->orderTotalByTime(Time::dayToNow(30)[0]);
    	$this->assign("data",$data);
    	return $this->fetch();
    }

    /**
     * @Title: orderTotalByTime
     * @Description: todo(根据订单时间获取订单总数量)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    private function orderTotalByTime($time)
    {
        $where = [
        	['create_time','>=',$time],
        ];
        $total = $this->cModel->where($where)->count();
        return $total;
    }

    /**
     * @Title: OrderAmountByTime
     * @Description: todo(根据订单时间获取订单成交总金额)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    private function orderAmountByTime($time)
    {
        $where = [
        	['create_time','>=',$time],
        	['pay_time','<>',0],
        ];
        $total = $this->cModel->where($where)->sum("amount");
        return $total;
    }

    /**
     * @Title: orderTotal
     * @Description: todo(订单总数量)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    private function orderTotal()
    {
        $total = $this->cModel->count();
        return $total;
    }

    /**
     * @Title: OrderAmount
     * @Description: todo(总成交金额)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    private function orderAmount()
    {
        $where = [
        	['pay_time','<>',0],
        ];
        $total = $this->cModel->where($where)->sum("amount");
        return $total;
    }

    /**
     * @Title: orderLine
     * @Description: todo(30天订单曲线图数据)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    private function orderLine($day = 15)
    {
        $data = [];
        $time = Time::dayToNow($day);
        $nowTime = $time[1];
        $LineTime = $time[0];
        for ($i=0; $i < $day; $i++) { 
        	$where = [];
        	$time = $LineTime+86400*$i;
        	$labels[] = date("Y-m-d",$time);
        	$where[] = ['create_time', 'between', [$time , $time+86400]];
        	$num[] = $this->cModel->where($where)->count();
        	$where[] = ['pay_time', '<>', 0];
        	$amount[] = $this->cModel->where($where)->sum('amount');
        }
        $data['labels'] = $labels;
        $data['datasets'] = [
        	[
        		'label' => '下单数量',
        		'fill'  => false,
        		'borderColor' => '#b8f3e2',
        		'data' => $num
        	],
        	[
        		'label' => '成交金额',
        		'fill'  => false,
        		'borderColor' => '#87ceed',
        		'data' => $amount
        	],
        ];
        return json_encode($data);
    }

    /**
     * @Title: detail
     * @Description: todo(订单详情)
     * @author 
     * @date 2019年6月13日
     * @throws
     */
    public function detail($ordersn){
        $where[] = ['ordersn','=',$ordersn];
        $data = $this->cModel->where($where)->find();
        $this->assign("data",$data);
        return $this->fetch();
    }
}